﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FlickrNet;
using SHDocVw;
using System.Threading;
using System.Reflection;

namespace Get_Authentication_Token
{
    public partial class GetAuthToken : Form
    {
        Flickr flickr;
        string tempFrob = null;
        ErrorProvider errorProvider = new ErrorProvider();

        public GetAuthToken()
        {
            InitializeComponent();
            this.Text += String.Format(" ({0}.{1}.{2})", Assembly.GetExecutingAssembly().GetName().Version.Major, Assembly.GetExecutingAssembly().GetName().Version.Minor, Assembly.GetExecutingAssembly().GetName().Version.Build);
            step1Button.Click += new EventHandler(step1Button_Click);
            step2Button.Click += new EventHandler(step2Button_Click);
        }

        void step2Button_Click(object sender, EventArgs e)
        {
            try
            {
                Auth auth = flickr.AuthGetToken(tempFrob);
                authTokenTextBox.Text = auth.Token;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "step2Button_Click()", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        void step1Button_Click(object sender, EventArgs e)
        {
            try
            {
                errorProvider.Dispose();

                if (ValidateFormData())
                {
                    flickr = new Flickr(apiKeyTextBox.Text, sharedSecretTextBox.Text);
                    tempFrob = flickr.AuthGetFrob();
                    string flickrUrl = flickr.AuthCalcUrl(tempFrob, AuthLevel.Read);
                    NavigateUsingIE(flickrUrl);
                    step2Button.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "step1Button_Click()", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void NavigateUsingIE(string url)
        {
            try
            {
                InternetExplorer ieObj = new InternetExplorer();
                object o = null;
                ieObj.Offline = false;
                ieObj.Visible = true;
                ieObj.Navigate(url, ref o, ref o, ref o, ref o);

                //while (ieObj.Busy)
                //{
                //    Thread.Sleep(1000);
                //}

                //ieObj.Quit();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "NavigateUsingIE()", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        bool ValidateFormData()
        {
            try
            {
                if (apiKeyTextBox.Text.Trim().Length == 0)
                {
                    errorProvider.SetError(apiKeyTextBox, "Please specify the API Key.");
                    apiKeyTextBox.Select();
                    return false;
                }

                if (sharedSecretTextBox.Text.Trim().Length == 0)
                {
                    errorProvider.SetError(sharedSecretTextBox, "Please specify the Shared Secret.");
                    sharedSecretTextBox.Select();
                    return false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ValidateFormData()", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            return true;
        }
    }
}
